package com.salinity.kun.algorithm.harmonic;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.salinity.kun.algorithm.harmonic.AvgEclipticLongitude.ELSymbol;
import com.salinity.kun.util.DateUtil;

/**
 * 交点因子与订正角的求取
 * 
 * @author Administrator
 *
 */
public class InterFactorAndCorAngle {
	private Map<TideSymbol, double[]> cacheMap;
	private LocalDateTime LDTime;
	private AvgEclipticLongitude AELongitude;

	public InterFactorAndCorAngle(Date date) {
		cacheMap = new HashMap<>();
		LDTime = DateUtil.Date2LDTime(date);
		AELongitude = new AvgEclipticLongitude(date);

	}

	/**
	 * 
	 * @param tideSymbol
	 * @return 0:交点因子；1：订正角,单位为度
	 */
	public double[] getValue(TideSymbol tideSymbol) {

		if (!cacheMap.containsKey(tideSymbol)) {
			double[] tempVal = new double[2];
			double N = AELongitude.getAELongitude(ELSymbol.N) * Math.PI / 180;
			switch (tideSymbol) {
			case M2:
				tempVal[0] = 1.0004 - 0.0373 * Math.cos(N) + 0.0002 * Math.cos(2 * N);
				tempVal[1] = -2.14 * Math.sin(N);
				break;
			case S2:
				tempVal[0] = 1;
				tempVal[1] = 0;
				break;
			case N2:
				tempVal = getValue(TideSymbol.M2);
				break;
			case K2:
				tempVal[0] = 1.0241 + 0.2863 * Math.cos(N) + 0.0083 * Math.cos(2 * N) - 0.0015 * Math.cos(3 * N);
				tempVal[1] = -17.74 * Math.sin(N) + 0.68 * Math.sin(2 * N) - 0.04 * Math.sin(3 * N);
			case K1:
				tempVal[0] = 1.006 + 0.115 * Math.cos(N) - 0.0088 * Math.cos(2 * N) + 0.0006 * Math.cos(3 * N);
				tempVal[1] = -8.86 * Math.sin(N) + 0.68 * Math.sin(2 * N) - 0.07 * Math.sin(3 * N);
				break;
			case O1:
				tempVal[0] = 1.0089 + 0.1871 * Math.cos(N) - 0.0147 * Math.cos(2 * N) + 0.0014 * Math.cos(3 * N);
				tempVal[1] = 10.8 * Math.sin(N) - 1.34 * Math.sin(2 * N) + 0.19 * Math.sin(3 * N);
				break;
			case P1:
				tempVal[0] = 1;
				tempVal[1] = 0;
				break;
			case Q1:
				tempVal = getValue(TideSymbol.O1);
				break;
			case M4:
				tempVal = getValue(TideSymbol.M2);
				tempVal[0] = tempVal[0] * tempVal[0];
				tempVal[1] = 2 * tempVal[1];
				break;
			case MS4:
				tempVal = getValue(TideSymbol.M2);
				break;
			case M6:
				tempVal = getValue(TideSymbol.M2);
				tempVal[0] = tempVal[0] * tempVal[0] * tempVal[0];
				tempVal[1] = 3 * tempVal[1];
				break;
			default:
				throw new RuntimeException("Have no InterFactor And CorAngle of " + tideSymbol.name());

			}
			cacheMap.put(tideSymbol, tempVal);
		}
		return cacheMap.get(tideSymbol);
	}

}
